package com.zhangxun.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangxun.domain.IndexImg;
import com.zhangxun.service.IndexImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@Api("轮播图管理接口")
@RestController
@RequestMapping("/admin/indexImg")
public class IndexImgController {

    @Autowired
    private IndexImgService indexImgService;


    @GetMapping("/page")
    @ApiOperation("分页查询轮播图接口")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public Page<IndexImg> indexImgpPage(Page<IndexImg> page,IndexImg indexImg){
        page = indexImgService.selectPage(page,indexImg);

        return page;
    }

    @PostMapping
    @ApiOperation("保存新增轮播图接口")
    @PreAuthorize("hasAuthority('admin:indexImg:save')")
    public ResponseEntity<Boolean> save(@RequestBody IndexImg indexImg){
            indexImg.setUploadTime(new Date());
            return ResponseEntity.ok(indexImgService.save(indexImg));
    }


    @GetMapping("/info/{indexId}")
    @ApiOperation("根据id查询接口")
    @PreAuthorize("hasAuthority('admin:indexImg:info')")
    public ResponseEntity<IndexImg> indexImgInfo(@PathVariable("indexId") Long indexId){

       IndexImg indexImg = indexImgService.selectById(indexId);

       return  ResponseEntity.ok(indexImg);
    }


    @GetMapping("/indexImgs")
    @ApiOperation("小程序查询所有的轮播图")
    public ResponseEntity<List<IndexImg>> indexImgs(){


        return  ResponseEntity.ok(indexImgService.list());
    }
}
